<?php
/**
 * Manejo de sentencias basicas en tablas
 *
 * @author drarko
 */
class table {

    protected $_name;
    protected $_conn;
    
    protected $_query;
    
    protected $_where = "";
    protected $_order = "";
    protected $_field = "";
    
    protected $_set = "";
    
    protected $_values = "";
    
    public function __construct($table_name)
    {
        $this->_conn = connect::getConnection("MySql");
        $this->_name = $table_name;
        return $this;        
    }
    
    
    public function addWhere($campo,$texto)
    {
        if($this->_where != "")
            $this->_where = $campo . " = " . $texto . " AND " . $this->_where;
        else
            $this->_where = $campo . " = " . $texto ;
    }
    
    public function addLikeWhere($campo,$texto)
    {
        if($this->_where != "")
            $this->_where = $campo . " LIKE " . $texto . " AND " . $this->_where;
        else
            $this->_where = $campo . " LIKE " . $texto;
    }
    
    public function setOrder($campo,$ord)
    {
        $this->_order = "ORDER BY ". $campo . " " . $ord;
    }
    public function addCampo($campo)
    {
        if($this->_field != "")
            $this->_field = $campo . "," . $this->_field;
        else
            $this->_field = $campo;
    }    
    
    public function select() 
    {
        
        $this->_query = "SELECT ";
        
        if($this->_field == "")
            $this->_field = "*";
       
        $this->_query = $this->_query . $this->_field . " FROM " . $this->_name;
        
        if($this->_where != "")
           $this->_where = "WHERE " . $this->_where;
        
        $this->_query = $this->_query . " " . $this->_where . " " . $this->_order;
        
        $result = $this->_conn->execute($this->_query);
        
        $this->_where = "";
        $this->_query = "";
        $this->_order = "";
        $this->_field = "";
        
        return $result;
    }
    
    
    public function addSet($campo, $valor)
    {
        if($this->_set != "")
            $this->_set = $campo . " = " . $valor . "," . $this->_set;
        else
            $this->_set = $campo . " = " . $valor;        
        
        
    }
    
    public function update()
    {
        $this->_query = "UPDATE " . $this->_name . " SET ";
        
        $this->_query = $this->_query . $this->_set;
        
        if($this->_where != "")
           $this->_where = "WHERE " . $this->_where;
        
        $this->_query = $this->_query . " " . $this->_where;
        $result = $this->_conn->execute($this->_query);
        
   
        $this->_where = "";
        $this->_query = "";
        $this->_set = "";
        
        return $result;
    }
    
    
    public function addValue($valor)
    {
        if($this->_values != "")
            $this->_values = $this->_values . "," . $valor;
        else
            $this->_values = $valor;        
        
        
    }
    
    public function insert()
    {
        
        $this->_query = "INSERT INTO " . $this->_name . " VALUES (" . $this->_values . ")";
        
        $result = $this->_conn->execute($this->_query);
        
        
        $this->_values = "";
        $this->_query = "";
        return $result;
    }
    public function delete()
    {
        $this->_query = "DELETE FROM " . $this->_name ;
             
        if($this->_where != "")
           $this->_where = "WHERE " . $this->_where;
        
        $this->_query = $this->_query . " " . $this->_where;
        
        $result = $this->_conn->execute($this->_query);
        
        $this->_where = "";
        $this->_query = "";
        
        return $result;
    }
    
    public function fechaActual()
    {
        date_default_timezone_set("America/Argentina/Buenos_Aires");
        
        return "'".date("Y-m-d H:i:s")."'";
        
    }
    
    public function borrar($id)
    {
        $this->addWhere("id", $id);
        
        $result = $this->delete();
        
        return $result;
        
    }  
    
    public function habilitar($id, $bool)
    {
        $this->addSet("habilitado", $bool);
        $this->addWhere("id" , $id);        
        $result = $this->update();
        return $result;
    }        

    public function export($archivo)
    {
        $a = fopen($archivo,  "w");
        
        $result = $this->select();
        
        foreach($result as $r)
        {
            foreach($r as $i)
            {
                fwrite($a,$i."\t");
            }
            fwrite($a,"\n");
        }
    
        fclose($a);
    }
    
}


